package com.testpro.java.Steam;

import cn.hutool.core.date.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.testng.Assert;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

import java.io.*;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;

/**
 * +--------------------+ +------+ +------+ +---+ +-------+ | stream of elements +-----> 筛选-->排序-->
 * |filter+->|sorted+-> |map+-> |collect| +--------------------+ +------+ +------+ +---+ +-------+
 *
 * @author Administrator testdevops
 * @description 流测试
 * @create 2019-07-08 12:58
 */
@Slf4j(topic = "世界上最靓的星")
public class SteamTest {
  List<Person> peoples = new ArrayList<>();

  public static final Integer ENDSIZE = 1000;

  @BeforeClass
  public void initData() {

    for (int i = 0; i < ENDSIZE; i++) {
      Person p = new Person();
      final Person father =
          new Person()
              .setName("father")
              .setFather(null)
              .setBirthday(DateUtil.parse("1986-03-01"))
              .setSally(15000.00)
              .setAge(50);
      final Person person =
          p.setAge(i)
              .setBirthday(DateUtil.parse("2010-03-01"))
              .setFather(father)
              .setSally(Double.valueOf(i))
              .setName("tom" + i);

      peoples.add(person);
    }
  }

  /** stream() − 为集合创建串行流。 */
  @Test
  public void helloWorldSteam() {
    final Stream<Person> stream = peoples.stream(); // 获取包装类型
    final long count = stream.count(); // 获取长度
    Assert.assertEquals(count, 1000);
    log.info(String.valueOf(count));

    final List<String> strings = Arrays.asList("a", "b");
    final Object[] objects = strings.toArray();
    final List<Object> objects1 = Arrays.asList(objects);
    System.out.println(objects1);
  }

  /** parallelStream() − 为集合创建并行流 */
  @Test
  public void helloWorldParallelStream() {
    final Stream<Person> stream = peoples.parallelStream(); // 获取包装类型

    final long count = stream.count(); // 获取长度
    Assert.assertEquals(count, 1000);
    log.info(String.valueOf(count));
  }

  /** CreateStreamByCollection */
  @Test
  public void CreateStreamByCollection() {

    Collection<String> collection = new ArrayList();
    // 创建一个流
    final Stream<String> stream = collection.stream();
    // 创建一个并行流
    final Stream<String> parallelStream = collection.parallelStream();
    // Arrays创建流
    String[] strings = {"a", "b", "c"};
    final Stream<String> ArraysStream = Arrays.stream(strings);
  }

  /** CreateStreamByBufferedReader 在本地c盘创建一个文件 a.txt 写几行文件内容；并使用流处理 循环打印 */
  @Test
  public void CreateStreamByBufferedReader() throws FileNotFoundException {
    String fileName = "c:/a.txt";
    BufferedReader bufferedReader =
        new BufferedReader(new InputStreamReader(new FileInputStream(new File(fileName))));
    final Stream<String> lines = bufferedReader.lines();
    final List<String> collect = lines.collect(Collectors.toList());
    // 姿势1
    collect.stream().forEach(System.out::println);
    // 姿势2
    collect.stream().forEach(s -> System.out.println(s));
    // 姿势3
    collect.stream()
        .forEach(
            s -> {
              System.out.println(s);
            });
  }

  /** CreateStreamByInStream */
  @Test
  public void CreateStreamByInStream() {
    final IntStream range = IntStream.range(0, 1000);
    final OptionalDouble average = range.average();
    log.info(average.toString());
  }
}
